<?php
/**
 * 分类缓存类
 * Class cache
 */
class cache
{
    var $filename = "";
	var $cachetype = 0;

    public function __construct($cachePath, $cacheName, $cachetype=0)
    {
      makeDirectory(PATH."./template/cache/$cachePath/");
	  $this->filename = PATH."./template/cache/$cachePath/".$cacheName;
	  $this->cachetype = $cachetype;
    }

    public function getcache()
    {
	  $fp = @fopen($this->filename,'r');	
	  $res = @fread($fp,filesize($this->filename));
	  fclose($fp);
      return $this->cachetype ? $res : unserialize($res);
    }

    public function setcache($res)
    {
	  $res = $this->cachetype ? $res : serialize($res);
      $fp = fopen($this->filename,'w');
	  flock($fp,2);
	  fwrite($fp,$res);
	  fclose($fp);
      return $res;
    }

    public function filetime()
    {
	  return @filemtime($this->filename);		
	}

    public function makeDirectory($directoryName)
    {
      $temp = $directoryName;
      if(!is_dir($temp)){
        $oldmask = umask(0);
        if(!mkdir($temp, 0777)) exit("不能建立目录 $temp");
        umask($oldmask);
      }
      return $temp;
    }
}
?>